﻿package com.moonshineproject.plugin.project.vo.flash {
	import com.moonshineproject.plugin.project.vo.FileWrapper;
	
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	
	import no.doomsday.console.ConsoleUtil;
	
	public class MXMLCConfigVO extends FileWrapper 
	{
		public function MXMLCConfigVO(file:File=null)
		{
			super(file);
		}
		
		public function write(pvo:AS3ProjectVO):void 
		{
			if (pvo.targets.length == 0) 
			{
				ConsoleUtil.addErrorMessage("No targets found for config construction.");
				return;
			}
			
			var i:int;
			
			var oldIC:Boolean = XML.ignoreComments;
			XML.ignoreComments = false;
			
			var data:XML = <flex-config />;
				
			XML.ignoreComments = oldIC;
			
			data.appendChild(
				<target-player>{pvo.swfOutput.swfVersion}.0.0</target-player>
			);

			data.appendChild(
				<compiler />
			);
			
			// Classpaths
			if (pvo.classpaths.length > 0) 
			{
				data.compiler.appendChild(
					<source-path append="true" />
				);
				
				var classpaths:XMLList = data.compiler["source-path"];
				for (i = 0; i < pvo.classpaths.length; i++) 
				{
					classpaths.appendChild(
						<path-element>{pvo.classpaths[i].nativePath}</path-element>
					);
				}
			}
			
			// Library paths (SWCs)
			if (pvo.libraryPaths.length > 0) 
			{
				data.compiler.appendChild(
					<library-path append="true" />
				);
				
				var libraryPaths:XMLList = data.compiler["library-path"];
				for (i = 0; i < pvo.libraryPaths.length; i++) 
				{
					libraryPaths.appendChild(
						<path-element>{pvo.libraryPaths[i].nativePath}</path-element>
					);
				}
			}
			
			// Target paths
			data.appendChild(
				<file-specs>
					<path-element>{pvo.targets[0].nativePath}</path-element>
				</file-specs>
			);
			
			// SWF
			data.appendChild(
				<default-background-color>#{pvo.swfOutput.backgroundColorHex}</default-background-color>
			);
			data.appendChild(
				<default-frame-rate>{pvo.swfOutput.frameRate}</default-frame-rate>
			);
			data.appendChild(
				<default-size>
					<width>{pvo.swfOutput.width}</width>
					<height>{pvo.swfOutput.height}</height>
				</default-size>
			);
			
			var dataStr:String = 	
				"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"+
				"<!--This Adobe Flex compiler configuration file was generated by a tool.-->\n"+
				"<!--Any modifications you make may be lost.-->\n"+
				data.toXMLString();
			
			//ConsoleUtil.print(dataStr); return;
			
			if (!file)
			{
				var dir:File = pvo.file.resolvePath("obj/");
				if (!dir.exists) dir.createDirectory();
				
				file = dir.resolvePath(pvo.projectName+"Config.xml");
			}
			
			ConsoleUtil.print("Updating config file for "+pvo.projectName+" in "+file.nativePath);
			
			var stream:FileStream = new FileStream();
			stream.open(file, FileMode.WRITE);
			stream.writeUTFBytes(dataStr);
			stream.close();
		}
	}
}